using System.Text.Json;
using Dapr.Client;
using Dcy.Dapr.Client.Models;
using Dcy.Dapr.RemoteServices;
using Microsoft.AspNetCore.Mvc;

namespace Dcy.Dapr.Client.Controllers;

/// <summary>
/// dapr run -a dapr-client -p 7098 -P https -H 3501 -G 35001   -- dotnet  run
/// dapr run -a dapr-client -p 7098 -P grpc -H 3501 -G 35001  --app-ssl -- dotnet run
/// </summary>
[ApiController]
[Route("api/[controller]")]
public class HomeController : ControllerBase
{
    private readonly DaprClient _daprClient;
    private readonly IConfiguration _configuration;
    private readonly ILogger<HomeController> _logger;

    public HomeController(ILogger<HomeController> logger, DaprClient daprClient, IConfiguration configuration)
    {
        _logger = logger;
        _daprClient = daprClient;
        _configuration = configuration;
    }

    [Route("Index")]
    [HttpGet]
    public async Task<ActionResult> Index()
    {
        var result =
            await _daprClient.InvokeMethodAsync<IEnumerable<WeatherForecast>>(HttpMethod.Get, "dapr-server",
                "WeatherForecast");
        _logger.LogInformation("Index {@result}", result);
        return Ok(result);
    }

    [HttpGet]
    [Route("Login")]
    public async Task<ActionResult> Login()
    {
        var result =
            await _daprClient.InvokeMethodGrpcAsync<LoginRemoteInDto, LoginRemoteOutDto>("dapr-server",
                "Login", new LoginRemoteInDto());
        _logger.LogInformation("Index {@result}", result);
        return Ok(result);
    }

    [HttpGet]
    [Route("GetConfig")]
    public async Task<ActionResult> GetConfig()
    {
        var config =
            await _daprClient.GetConfiguration("configStore", []);
        foreach (var item in config.Items)
        {
            var cfg = JsonSerializer.Serialize(item.Value);
            _logger.LogInformation("Configuration for " + item.Key + ": " + cfg);
        }

        return Ok(config);
    }

    [HttpGet]
    [Route("GetConfigNew")]
    public async Task<ActionResult> GetConfigNew()
    {
        var config = _configuration["orderId1"];

        return Ok(config);
    }

    [HttpGet]
    [Route("GetSecret")]
    public async Task<ActionResult> GetSecret()
    {
        var secret =
            await _daprClient.GetSecretAsync("localSecretStore", "redisPassword");
        _logger.LogInformation("Secret for {secretValue}", string.Join(", ", secret));
        return Ok(secret);
    }

    [HttpGet]
    [Route("GetSecretNew")]
    public async Task<ActionResult> GetSecretNew()
    {
        var secret = _configuration["connectionStrings:sql"];
        return Ok(secret);
    }

    [HttpGet]
    [Route("SaveState")]
    public async Task<ActionResult> SaveState()
    {
        await _daprClient.SaveStateAsync("localState", "1", "hello-drap");
        return Ok();
    }

    [HttpGet]
    [Route("GetState")]
    public async Task<ActionResult> GetState()
    {
        var result =
            await _daprClient.GetStateAsync<string>("localState", "1");
        _logger.LogInformation("GetState for {result}", string.Join(", ", result));
        return Ok(result);
    }

    [HttpGet]
    [Route("DeleteState")]
    public async Task<ActionResult> DeleteState()
    {
        await _daprClient.DeleteStateAsync("localState", "1");
        return Ok();
    }

    [HttpGet]
    [Route("PublishEvent")]
    public async Task<ActionResult> PublishEvent()
    {
        var order = new Order()
        {
            Id = "1",
            Name = "order1234"
        };
        await _daprClient.PublishEventAsync("orderpubsub", "orders", order);
        return Ok();
    }
}